from typing import *
from leetcode.editor.common.node import *


# leetcode submit region begin(Prohibit modification and deletion)
class MyCircularDeque(object):

    def __init__(self, k):
        self.data = [None] * k
        self.capacity = k
        self.size = 0
        # [start,end)
        self.start = 0  # 循环数组的开始节点 闭区间
        self.end = 0  # 开区间

    # def resize(self, new_size):
    #     new_data = [None] * new_size
    #     for i in range(self.size):
    #         new_data[i] = self.data[(self.start+i)%self.capacity]
    #     self.data = new_data
    #     self.capacity = new_size
    #     self.start = 0
    #     self.end = self.size

    def insertFront(self, value):
        if (self.size == self.capacity):
            return False
        self.start = (self.start - 1 + self.capacity) % self.capacity
        self.data[self.start] = value
        self.size += 1
        return True

    def insertLast(self, value):
        if (self.size == self.capacity):
            return False
        self.data[self.end] = value
        self.end = (self.end + 1 + self.capacity) % self.capacity

        self.size += 1
        return True

    def deleteFront(self):
        if (self.size == 0):
            return False
        self.data[self.start] = None
        self.start = (self.start + 1) % self.capacity
        self.size -= 1
        return True

    def deleteLast(self):
        if (self.size == 0):
            return False
        self.end = (self.end - 1) % self.capacity
        self.data[self.end] = None
        self.size -= 1
        return True

    def getFront(self):
        if self.data[self.start] ==None:
            return -1
        return self.data[self.start]

    def getRear(self):
        if self.data[(self.end - 1) % self.capacity] ==None:
            return -1
        return self.data[(self.end - 1) % self.capacity]

    def isEmpty(self):
        return self.size == 0

    def isFull(self):
        return self.size == self.capacity

# Your MyCircularDeque object will be instantiated and called as such:
# obj = MyCircularDeque(k)
# param_1 = obj.insertFront(value)
# param_2 = obj.insertLast(value)
# param_3 = obj.deleteFront()
# param_4 = obj.deleteLast()
# param_5 = obj.getFront()
# param_6 = obj.getRear()
# param_7 = obj.isEmpty()
# param_8 = obj.isFull()
# leetcode submit region end(Prohibit modification and deletion)


if __name__ == '__main__':
    solution = MyCircularDeque(3)
    print(solution.insertLast(1))
    print(solution.insertLast(2))
    print(solution.insertFront(3))
    print(solution.insertFront(4))
    print(solution.getRear())
